package com.newland.esp.controller.user;

import com.alibaba.fastjson.JSONObject;
import com.newland.esp.common.constant.RedisKeyConstant;
import com.newland.esp.common.exception.code.BaseResponseCode;
import com.newland.esp.common.util.DataResult;
import com.newland.esp.common.util.login.TokenUtil;
import com.newland.esp.entity.user.AdminInfoEntity;
import com.newland.esp.service.user.SSOService;
import com.newland.esp.vo.response.user.loginInfoResp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @param
 * @author lw
 * @descripe
 * @date 2021/8/14 18:01
 * @Version 1.0
 **/
@Controller
@CrossOrigin
@RequestMapping("/user")
public class SSOController {


    @Autowired
    private SSOService ssoService;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;
    /**
     *  用户登录
     * @param admin
     * @return
     */
    @RequestMapping(value = "/login")
    @ResponseBody
    public DataResult login(AdminInfoEntity admin, String vertify) {
//         获取验证码
       String capText  = redisTemplate.opsForValue().get(RedisKeyConstant.USER_VERIFICATION);
//         验证码是否一致，不一致返回提示信息
        if (!(vertify.trim().equals(capText))) {

            return DataResult.getResult(BaseResponseCode.PASS_ERROR_03.getCode(),BaseResponseCode.PASS_ERROR_03.getMsg());
        }
        // 登录并生成票据
        String ticket = ssoService.login(admin);
        if (null != ticket && ticket.length() > 0) {
            String user = redisTemplate.opsForValue().get(RedisKeyConstant.USER_TICKET + ":" + ticket);
            AdminInfoEntity adminEntity = JSONObject.parseObject(user, AdminInfoEntity.class);

            loginInfoResp loginInfo = new loginInfoResp();
            loginInfo.setUserInfo(adminEntity);
            loginInfo.setToken(ticket);

            return  DataResult.success(loginInfo);
        }
        // 用户名或者密码错误
        return DataResult.getResult(BaseResponseCode.PASS_ERROR_04.getCode(),BaseResponseCode.PASS_ERROR_04.getMsg());
    }

    /**
     * 根据token后取用户信息
     */
    @RequestMapping(value = "/getInfo")
    @ResponseBody
    public DataResult userInfo(String token) {
        String user = redisTemplate.opsForValue().get(RedisKeyConstant.USER_TICKET + ":" + token);
        AdminInfoEntity adminEntity = JSONObject.parseObject(user, AdminInfoEntity.class);
        return  DataResult.success(adminEntity);
    }

    /**
     * 用户退出
     *
     *
     * @return
     */
    @PostMapping("/logout")
    @ResponseBody
    public DataResult logout(HttpServletRequest request) {
        String userTicket = TokenUtil.getRequestToken(request);
//        String userTicket = (String)ThreadLocalUtils.getMap("userTicket");
        System.out.println("userticket: " + userTicket);
        //清除redis
        Boolean logout = ssoService.logout(userTicket);
        if (logout == true){

            return DataResult.success();
        }
        return DataResult.error();
    }
}
